﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IHISCETeim.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using IHISCETeim.Model;
using System.Data.SqlClient;
namespace IHISCETeim.DAL
{
    public class FXSJZYDAL
    {
        /// <summary>
        /// 三井信友逾期情况，删除
        /// 当导入前操作
        /// </summary>
        /// <returns></returns>
        public static string Clear()
        {
            Database DB = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = DB.GetStoredProcCommand("FXSJZYAgent_Clear");
            string x = "";
            try
            {
                DB.ExecuteScalar(dbCommand);
                x="";
            }
            catch (Exception exc)
            {
                x = exc.Message;
            }
            return x;
        }

        /// <summary>
        /// 三井住友逾期情况-经销商
        /// 导入
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int FXSJZYAgentImportFromExecl(FXSJZYAgentInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = DB.GetStoredProcCommand("FXSJZYAgent_ImportFromExecl",obj.AliasName,obj.CardName,obj.TotalRecovery,obj.OverdueAmount,obj.RecoveryRent,obj.YuQiLv,obj.NotRecoveryRent,obj.OverdueRatio,obj.Range3,obj.Range1,obj.Goal,obj.CreateDate);
            int x = 0;
            try
            {
                x=Convert.ToInt32( DB.ExecuteScalar(dbCommand));
            }
            catch (Exception exc)
            {
                x =0;
            }
            return x;
        }
        /// <summary>
        /// 三井住友逾期情况-经销商
        /// 根据条件获取
        /// </summary>
        /// <param name="sqlwhere"></param>
        /// <returns></returns>
        public static DataTable FXSJZYAgentGetByWhere(string sqlwhere)
        {
            Database DB = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = DB.GetStoredProcCommand("FXSJZYAgent_GetByWhere", sqlwhere);
            return DB.ExecuteDataSet(dbCommand).Tables[0];
        }


        /// <summary>
        /// 三井住友逾期情况-客户逾期明细
        /// 导入
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int FXSJZYDetailImportFromExecl(FXSJZYDetailInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = DB.GetStoredProcCommand("FXSJZYDetail_ImportFromExcel", obj.AliasName, obj.CardName, obj.CustomerName, obj.ContractNo, obj.Balance1, obj.Balance2, obj.Balance3,obj.OverdueAmount,obj.OverdueNumber);
            int x = 0;
            try
            {
                x = Convert.ToInt32(DB.ExecuteScalar(dbCommand));
            }
            catch (Exception exc)
            {
                x = 0;
            }
            return x;
        }

        public static DataTable FXSJZYDetailGetPagination(string WhereSql, int PageIndex, int PageSize, string Sort, ref int TotalCount)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("FXSJZYDetail_GetPagination");
            DB.AddInParameter(dbCommand, "sqlwhere", DbType.String, WhereSql);
            DB.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            DB.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            DB.AddInParameter(dbCommand, "Sort", DbType.String, Sort);
            DB.AddOutParameter(dbCommand, "TotalCount", DbType.Int32, 8);
            DataTable dt = DB.ExecuteDataSet(dbCommand).Tables[0];

            TotalCount = Convert.ToInt32(DB.GetParameterValue(dbCommand, "TotalCount"));
            return dt;
        }

        public static string FXSJZYAgent_GetUpdateDate()
        {
            Database DB = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = DB.GetStoredProcCommand("FXSJZYAgent_GetUpdateDate");
            return Convert.ToDateTime( DB.ExecuteScalar(dbCommand)).ToString("yyyy-MM-dd");
        }

    }
}
